package com.shenfa.action.systemManager;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.shenfa.action.BaseAction;
import com.shenfa.bean.Emprole;
import com.shenfa.service.systemManager.role.RoleService;

import com.shenfa.util.ResultObject;

/**
 * 角色
 * @author jiangmaochun
 *
 */
public class RoleAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private RoleService roleService;
	private static Logger log=Logger.getLogger(RoleAction.class);
	private Emprole role;
	//分页
	private int start;
	private int limit;
	
	/**
	 * 验证角色名是否存在
	 * @return
	 */
	public String checkRoleName(){
		String flag = roleService.checkRoleName(role);
		if("0".equals(flag)) {
			result=ResultObject.build("0",null);
		} else {
			result=ResultObject.build("1",null);
		}
		return SUCCESS;
	}
	
	/**
	 * 验证角色是否绑定用户
	 * @return
	 */
	public String checkDelRole(){
		String flag = roleService.checkDelRole(role);
		if("0".equals(flag)) {
			result=ResultObject.build("0",null);
		} else {
			result=ResultObject.build("1",null);
		}
		return SUCCESS;
	}
	
	/**
	 * 根据部门ID获取角色信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listByDepID(){
		if(0==limit){
			limit=1000;
		}
		if(null==role){
			return null;
		}else{
			log.debug("Did="+role.getDid());
			List<Emprole> list = roleService.getEmproleList(role.getDid(), start, limit);
			result=ResultObject.build("0",list);
			result.put("total", roleService.getCount(role.getDid()));
		}
		return SUCCESS;
	}
	
	/**
	 * 添加一个角色
	 * @return
	 */
	public String add(){
		//这个一个角色
		log.debug("Did="+role.getDid()+",Dname="+role.getDname()+",Oid="+role.getOid()+",Olddid="+role.getOlddid()
				+",Oname="+role.getOname()+",Pid="+role.getPid()+",Pname="+role.getPname()+",remark="+role.getRemark()
				+",Rid="+role.getRid()+",Rname="+role.getRname()+",roleFun="+role.getRolefun());
		String r=roleService.addRole(role);
		result=ResultObject.build("0",r);
		return SUCCESS;
	}
	
	public String updata(){
		log.debug("Did="+role.getDid()+",Dname="+role.getDname()+",Oid="+role.getOid()+",Olddid="+role.getOlddid()
				+",Oname="+role.getOname()+",Pid="+role.getPid()+",Pname="+role.getPname()+",remark="+role.getRemark()
				+",Rid="+role.getRid()+",Rname="+role.getRname()+",roleFun="+role.getRolefun());
		roleService.updataRole(role);
		result=ResultObject.build("0","0");
		return SUCCESS;
	}
	/**
	 * 删除一个角色
	 * @return
	 */
	public String del(){
		log.debug("Did="+role.getDid());
		int res=roleService.delRole(role.getRid());
		if(0!=res){
			result=ResultObject.build("1",res+"");
		} else{
			result=ResultObject.build("0","0");
		}
		return SUCCESS;
	}
	
	
	public void setRole(Emprole role) {
		this.role = role;
	}

	public Emprole getRole() {
		return role;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
}
